﻿
//格式化菜单栏
puxu.EdPuXu.prototype.menuFormat = function () {
	let me = this, notble = 'undo|redo';
	this.menuHTML();
	this.Tool_body.innerHTML = ''; //防止重复创建
	let toolbars = me.options.toolbars;
	//添加菜单
	for (var m = 0; m < toolbars.length; m++) {
		let clName = 'literm', div = me.c('div'), Item = toolbars[m];
		//分割线
		if (Item == 'line') {
			div.className = 'separator';
			this.Tool_body.appendChild(div);
			continue;
		}
		//处理菜单
		if (Item.length > 1) {
			clName += " jus_between";
			if (m > 1) clName += " operable";
		}
		div.className = clName;
		for (var i = 0; i < Item.length; i++) {
			if (this.notMenu.indexOf(Item[i]) == -1) {
				let list = me.c('div'), css = 'list';
				if (Item.length > 1) css += ' lst-' + Item.length;
				if (!(clName.indexOf("operable") != -1) && !(notble.indexOf(Item[i]) != -1)) css += ' operable';
				list.className = css;
				list.appendChild(this.menuList[Item[i]]);
				div.appendChild(list);
				if (this.menuList[Item[i]].success) {
					this.menuList[Item[i]].success(this.menuList[Item[i]], me);
				}
			}
		}
		this.Tool_body.appendChild(div);
	}
	let div = me.c('div');
	div.style.display = 'none';
	div.className = 'pux-disabledMask';
	this.Tool_body.appendChild(div);
};

//工具栏菜单HTML
puxu.EdPuXu.prototype.menuHTML = function () {
	//查看源码
	this.createMenu({ title: '源代码', name: 'source', css: 'icot ico_html', data: 'source' });
	//撤销
	this.createMenu({ title: '撤销', menu: '撤销上一步', name: 'undo', css: 'icot ico_retreat', data: 'undo' });
	//撤销
	this.createMenu({ title: '重做', menu: '重做下一步', name: 'redo', css: 'icot ico_redo', data: 'redo' });
	//复制
	this.createMenu({ title: '复制', name: 'copy', data: 'copy' });
	//粘贴
	this.createMenu({ title: '粘贴', name: 'paste', data: 'paste' });
	//剪切
	this.createMenu({ title: '剪切', name: 'cut', data: 'cut' });
	//插入文本块
	this.createMenu({
		title: '插入文本块', name: 'inserttextblock', dropdown: ToolEvent.ToolbarsDropdown('inserttextblock'), success: function (e, z) {
			setTimeout(function () { ToolEvent.SuccessFun('inserttextblock', e, z) }, 500);
		}
	});
	//插入图片
	this.createMenu({
		title: '插入图片', name: 'insertimage', css: 'icol ico_picture', id: 'image_' + this.uid, popup: ToolEvent.PopupContent('insertimage'), success: function (e, z) {
			setTimeout(function () { ToolEvent.SuccessFun('insertimage', e, z) }, 500);
		}
	});
	//插入表格
	this.createMenu({
		title: '插入表格', menu: '表格', name: 'inserttable', css: 'ifill ico_table', dropdown: ToolEvent.ToolbarsDropdown('inserttable'), success: function (e, z) {
			setTimeout(function () { ToolEvent.SuccessFun('inserttable', e, z); }, 500);
		}
	});
	//插入形状
	this.createMenu({ title: '插入形状', menu: '形状', name: 'insertshape', css: 'ifill ico_shape', dropdown: ToolEvent.ToolbarsDropdown('insertshape'), success: function (e, z) { ToolEvent.SuccessFun('insertshape', e, z) } });
	//填充
	this.createMenu({ title: '填充' ,name: 'backcolor', css: 'ifill ico_filling',data: '', dropdown: ToolEvent.ToolbarsDropdown('backcolor'),success: function (e, z) { ToolEvent.SuccessFun('backcolor', e, z) } });
	//边框
	this.createMenu({ title: '边框', name: 'bordercolor', css: 'ifill ico_frame', dropdown: ToolEvent.ToolbarsDropdown('bordercolor'),success: function (e, z) { ToolEvent.SuccessFun('bordercolor', e, z) } });
	//设置区块层级
	this.createMenu({ title: '设置区块层级', menu: '层级', name: 'hierarchy', css: 'icet icol ico_hierarchy', dropdown: ToolEvent.ToolbarsDropdown('hierarchy') });
	//字体
	this.createMenu({ title: '字体', name: 'fontfamily', css: 'btn_orange icet', dropdown: ToolEvent.ToolbarsDropdown('fontfamily'), success: function (e, z) { ToolEvent.SuccessFun('fontfamily', e, z) } });
	//字号
	this.createMenu({ title: '字号', name: 'fontsize', css: 'btn_orange icet', dropdown: ToolEvent.ToolbarsDropdown('fontsize'), success: function (e, z) { ToolEvent.SuccessFun('fontsize', e, z) } });
	//加粗
	this.createMenu({ title: '加粗', name: 'bold', data: 'bold', css: 'btn_link icot ico_Bold' });
	//倾斜
	this.createMenu({ title: '倾斜', name: 'italic', data: 'italic', css: 'btn_link icot ico_Italics' });
	//下划线
	this.createMenu({ title: '下划线', name: 'underline', data: 'underline', css: 'btn_link icot ico_underline' });
	//删除线
	this.createMenu({ title: '删除线', name: 'strikethrough', data: 'strikethrough', css: 'btn_link icot ico_line-through' });
	//文字颜色
	this.createMenu({ title: '文字颜色', name: 'forecolor', css: 'btn_link icot ico_colorlump', dropdown: ToolEvent.ToolbarsDropdown('forecolor'), success: function (e, z) { ToolEvent.SuccessFun('forecolor', e, z) } });
	//增加字号
	this.createMenu({ title: '增加字号', name: 'addfontsize', css: 'btn_link icot ico_Addsize' , dropdown: ToolEvent.ToolbarsDropdown('addfontsize'), success: function (e, z) { ToolEvent.SuccessFun('addfontsize', e, z) }});
	//减小字号
	this.createMenu({ title: '减小字号', name: 'reducefontsize', css: 'btn_link icot ico_Reducesize',dropdown: ToolEvent.ToolbarsDropdown('reducefontsize'), success: function (e, z) { ToolEvent.SuccessFun('reducefontsize', e, z) } });

	//对齐
	this.createMenu({ title: '对齐', name: 'alignment', css: 'btn_link icot ico_Alignleft', dropdown: ToolEvent.ToolbarsDropdown('alignment'), success: function (e, z) { ToolEvent.SuccessFun('alignment', e, z) } });
	//文字方向
	this.createMenu({ title: '文字方向', name: 'textdirection', css: 'btn_link icot ico_Textleft', dropdown: ToolEvent.ToolbarsDropdown('textdirection') });
	//字间距
	this.createMenu({ title: '字间距', name: 'wordspacing', css: 'btn_link icot ico_spacing', dropdown: ToolEvent.ToolbarsDropdown('wordspacing'), success: function (e, z) { ToolEvent.SuccessFun('wordspacing', e, z) } });
	//行高
	this.createMenu({ title: '行高', name: 'lineheight', data: 'lineheight', css: 'btn_link icot ico_line-height', dropdown: ToolEvent.ToolbarsDropdown('lineheight') });
	//首行缩进
	this.createMenu({ title: '首行缩进', name: 'indent', data: 'indent', css: 'btn_link icot ico_text-indent' });

	//居左对齐
	this.createMenu({ title: '居左对齐', name: 'justifyleft', data: 'justifyleft', css: 'btn_link icot ico_AlignTextLeft' });
	//居中对齐
	this.createMenu({ title: '居中对齐', name: 'justifycenter', data: 'justifycenter', css: 'btn_link icot ico_AlignTextCenter' });
	//居右对齐
	this.createMenu({ title: '居右对齐', name: 'justifyright', data: 'justifyright', css: 'btn_link icot ico_AlignTextRight' });

	//两端对齐
	this.createMenu({ title: '两端对齐', name: 'justifyjustify', data: 'justifyjustify', css: 'btn_link icot ico_AlignBothEnds' });
	//分散对齐
	this.createMenu({ title: '分散对齐', name: 'justifydistribute', data: 'justifydistribute', css: 'btn_link icot ico_AlignDisperse' });

	//靠上对齐
	this.createMenu({ title: '靠上对齐', name: 'verticaltop', data: 'verticaltop', css: 'btn_link icot ico_AlignTextTop' });
	//靠中对齐
	this.createMenu({ title: '靠中对齐', name: 'verticalcenter', data: 'verticalcenter', css: 'btn_link icot ico_AlignCenterText' });
	//靠下对齐
	this.createMenu({ title: '靠下对齐', name: 'verticaldown', data: 'verticaldown', css: 'btn_link icot ico_AlignTextDown' });
};

//设置菜单的功能
puxu.EdPuXu.prototype.menuAction = function () {
	var menu = this.Tool_body.getElementsByClassName('pux_btn');
	var me = this;
	console.log(me,"me");
	for (var i = 0; i < menu.length; i++) {
		menu[i].e = this;
		menu[i].attr = menu[i].getAttribute('data');
		if (menu[i].attr) {
			menu[i].onclick = function () {
				//anchorNode 返回选中内容前节点内的内容
				switch (this.attr) {
					//删除线
					case 'strikeThrough':
						var parent = me.range.anchorNode.parentNode;
						if (parent.style.textDecoration == 'line-through') {
							var content = me.range.anchorNode.parentNode.innerHTML;
							parent.parentNode.removeChild(parent);
							me.setText(content, true);
						} else {
							var a = me.c('span');
							a.style.textDecoration = 'line-through';
							me.setHTML(a);
						}
						break;
					//查看源代码
					case 'source':
						var d = me.Tool_body.getElementsByClassName('pux-disabledMask')[0];
						me.code = me.code ? 0 : 1;
						if (me.code) {
							me.Tool_body.className = 'Tool_body pux-noselect';
							d.style.display = 'block';
							this.className = 'pux_btn icot ico_html pux-active';
							me.d_left.body.className = 'pux-code';
							me.d_right.body.className = 'pux-code';
							var pre_left = me.d_left.body.getElementsByTagName('pre');
							var pre_right = me.d_right.body.getElementsByTagName('pre');
							//处理pre标签
							for (var s = 0; s < pre_left.length; s++) pre_left[s].innerHTML = pre_left[s].innerHTML.replace(/<\/*br>/g, "\n");
							for (var s = 0; s < pre_right.length; s++) pre_right[s].innerHTML = pre_right[s].innerHTML.replace(/<\/*br>/g, "\n");
							var text = me.getHTML('w_left');
							//格式化段落
							text = text.replace(/<\/p><p>/gim, "<\/p>\n<p>").replace(/><pre/gim, ">\n<pre").replace(/<\/pre></gim, "<\/pre>\n<");
							me.d_left.body.innerHTML = me.unhtml(text);
							text = me.getHTML('w_right');
							//格式化段落
							text = text.replace(/<\/p><p>/gim, "<\/p>\n<p>").replace(/><pre/gim, ">\n<pre").replace(/<\/pre></gim, "<\/pre>\n<");
							me.d_right.body.innerHTML = me.unhtml(text);
						} else {
							me.Tool_body.className = 'Tool_body pux-noselect';
							d.style.display = 'none';
							this.className = 'pux_btn icot ico_html';
							me.d_left.body.className = '';
							me.d_right.body.className = '';
							var text = me.getHTML('w_left');
							me.d_left.body.innerHTML = me.html(text);
							var pre_left = me.d_left.body.getElementsByTagName('pre');
							for (var s = 0; s < pre_left.length; s++) pre_left[s].innerHTML = pre_left[s].innerHTML.replace(/\n/g, "<br>");
							text = me.getHTML('w_right');
							me.d_right.body.innerHTML = me.html(text);
							var pre_right = me.d_right.body.getElementsByTagName('pre');
							for (var s = 0; s < pre_right.length; s++) pre_right[s].innerHTML = pre_right[s].innerHTML.replace(/\n/g, "<br>");
						}
						break;
					//默认执行execCommand
					default:
						var b = this.attr.split('|');
						/*console.log(puxu.Editarea);*/
						if (puxu.Editarea == 'w_left') {
							if (!me.w_left.document._useStyleWithCSS) {
								me.w_left.document.execCommand('styleWithCSS', null, true);
								me.w_left.document._useStyleWithCSS = true;
							}
							if (b.length > 1) {
								me.w_left.document.execCommand(b[0], false, b[1]);
							} else {
								me.w_left.document.execCommand(b[0], false, null);
							}
						} else {
							if (!me.w_right.document._useStyleWithCSS) {
								me.w_right.document.execCommand('styleWithCSS', null, true);
								me.w_right.document._useStyleWithCSS = true;
							}
							if (b.length > 1) {
								me.w_right.document.execCommand(b[0], false, b[1]);
							} else {
								me.w_right.document.execCommand(b[0], false, null);
							}
						}
					//me.range.getRangeAt(0).collapse(); //取消选中状态
				}
				return false;
			}
		}
	}
};

/**
 * 设置菜单底部样式
 * @method ToolFootFormat
 * @warning 设置菜单底部操作编辑区状态，使用说明。
 */
puxu.EdPuXu.prototype.ToolFootFormat = function () {
	var me = this;
	me.Tool_foot.innerHTML = ''; //防止重复创建
	let jusdiv = me.c('div');
	jusdiv.className = 'jus_between';
	let toolfoot = me.options.toolfoot;

	//添加操作区切换按钮
	for (var m = 0; m < toolfoot.length; m++) {
		let clName = 'list', div = me.c('div'), but = me.c('button'), Item = toolfoot[m];
		div.className = clName;
		if (m == 0) div.className += ' active';
		div.setAttribute('data-iframe', Item);

		but.type = 'button';
		but.className = 'pux_btn btn_default';
		if (Item == 'Editiframeleft') but.innerHTML = '左区'; else but.innerHTML = '右区'; 

		but.addEventListener('click', function () {
			$(this).parent('div').siblings('div.list').removeClass('active');
			$(this).parent('div').addClass('active');
			//切换处理事件
			if ($(this).parent('div').attr('data-iframe') == 'Editiframeleft') {
				puxu.Editarea = "w_left";
			} else {
				puxu.Editarea = "w_right";
			}
			me.init(puxu.Editarea);   //初始化参数
		});
		div.appendChild(but);
		jusdiv.appendChild(div);
	}

	me.Tool_foot.appendChild(jusdiv);

	jusdiv = me.c('div');
	jusdiv.className = 'instructions';
	jusdiv.innerHTML = '<a href="' + me.options.PuXu_HOME_URL + 'help.html" target="_blank">使用说明</a>';
	me.Tool_foot.appendChild(jusdiv);
}